package com.sunday.player.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.sunday.player.R;
import com.sunday.player.domain.musiclist;
import com.sunday.player.interfaceutil.interface_AdapterCallback;

public class adapter_musiclist extends ArrayAdapter<musiclist> {

    
    private LayoutInflater LI;
    private interface_AdapterCallback adapterCallback;
    public static final int Actoin_addplaylist = 0;
    
    public adapter_musiclist(Context context, int textViewResourceId,List<musiclist> objects, interface_AdapterCallback adapterCallback) {
	super(context, textViewResourceId, objects);
	LI = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	this.adapterCallback = adapterCallback;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
	ViewHolder holder = null;
	View itemView =  convertView;
	if(itemView == null){
	    itemView = LI.inflate(R.layout.item_musiclist, null);
	    holder = new ViewHolder();
	  
	    holder.CB_select 	= (CheckBox)itemView.findViewById(R.id.checkBox_folder);
	    holder.TV_foldername= (TextView)itemView.findViewById(R.id.textView_FolderName);
	    holder.TV_Counter	= (TextView)itemView.findViewById(R.id.textView_Count);
	    holder.IB_playlist_add = (ImageButton)itemView.findViewById(R.id.imageButton_add);
	    itemView.setTag(holder);
	}else{
	    holder = (ViewHolder)itemView.getTag();
	}
	
	final musiclist folder  = (musiclist)getItem(position);	
	holder.CB_select.setOnCheckedChangeListener(null);
	holder.CB_select.setChecked(folder.isCheck());
	holder.CB_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	    
	    @Override
	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		folder.setCheck(isChecked);
	    }
	});
	holder.IB_playlist_add.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		adapterCallback.onItemClickCallback(position, 0, Actoin_addplaylist);
	    }
	});
	
	holder.TV_foldername.setText(folder.getListName());
	holder.TV_Counter.setText(folder.getMusiclist().size()+"");
	return itemView;
    }
    private static class ViewHolder{
	
	CheckBox CB_select;
	TextView TV_foldername;
	TextView TV_Counter;
	ImageButton IB_playlist_add;
    }
}
